package com.lxm.ymcc.service.impl;

import com.alipay.easysdk.factory.Factory;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.lxm.ymcc.domain.PayOrder;
import com.lxm.ymcc.mapper.PayOrderMapper;
import com.lxm.ymcc.service.IPayOrderService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whale
 * @since 2022-11-04
 */
@Service
@Slf4j
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder> implements IPayOrderService {

    @Override
    public PayOrder selectOneSn(String orderNo) {

        Wrapper<PayOrder> wrapper = new EntityWrapper<>();
        wrapper.eq("order_no",orderNo);
       return  selectOne(wrapper);

    }

    @Override
    public void cancelOrder(String orderSn) {
        //1.根据订单号查询支付单
        PayOrder payOrder = selectOneSn(orderSn);
        if (payOrder!=null&&payOrder.getPayStatus().equals(PayOrder.PAY_PAYMENT)){
            //取消支付单
            payOrder.setPayStatus(PayOrder.PAY_TIMEOUT_CANCEL);
            payOrder.setUpdateTime(new Date());
            updateById(payOrder);
            try {
                //2.支付宝关单
                Factory.Payment.Common().close(orderSn);
            } catch (Exception e) {
                e.printStackTrace();
                log.info("支付宝关单失败：{}",e.getMessage());
            }
        }

    }
}
